/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cloudfactory.web;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactroyOrder;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactroyOrderService;

/**
 * 
* @ClassName: CloudfactroyOrderController
* @Description: 后台订单管理
* @author Stone
* @date 2018年9月8日
*
 */
@Controller
@RequestMapping(value = "${adminPath}/cloudfactory/cloudfactroyOrder")
public class CloudfactroyOrderController extends BaseController {

	@Autowired
	private CloudfactroyOrderService cloudfactroyOrderService;
	
	@ModelAttribute
	public CloudfactroyOrder get(@RequestParam(required=false) String id) {
		CloudfactroyOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = cloudfactroyOrderService.get(id);
		}
		if (entity == null){
			entity = new CloudfactroyOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("cloudfactory:cloudfactroyOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(CloudfactroyOrder cloudfactroyOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CloudfactroyOrder> page = cloudfactroyOrderService.findPage(new Page<CloudfactroyOrder>(request, response), cloudfactroyOrder); 
		model.addAttribute("page", page);
		return "cloudfactory/cloudfactroyOrderList";
	}

	@RequiresPermissions("cloudfactory:cloudfactroyOrder:view")
	@RequestMapping(value = "form")
	public String form(CloudfactroyOrder cloudfactroyOrder, Model model) {
		List<CloudfactroyOrder> cloudfactroyOrders = cloudfactroyOrderService.findByOrderNo(cloudfactroyOrder);
		model.addAttribute("cloudfactroyOrder", cloudfactroyOrders);
//		<th>企业名称</th>
//		<th>订单编号</th>
//		<th>购买用户</th>
//		<th>购买时间</th>
//		<th>审核状态</th>
//		<th>订单总额</th>
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		model.addAttribute("name", cloudfactroyOrders.get(0).getCompanyName());
		model.addAttribute("orderNo", cloudfactroyOrders.get(0).getOrderNo());
		model.addAttribute("userName", cloudfactroyOrders.get(0).getUserName());
		model.addAttribute("date", time.format(cloudfactroyOrders.get(0).getCreateDate()));
		model.addAttribute("status", cloudfactroyOrders.get(0).getStatus());
		model.addAttribute("totalMoney", cloudfactroyOrders.get(0).getTotalMoney());
		return "cloudfactory/cloudfactroyOrderForm";
	}

	@RequiresPermissions("cloudfactory:cloudfactroyOrder:edit")
	@RequestMapping(value = "save")
	public String save(CloudfactroyOrder cloudfactroyOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, cloudfactroyOrder)){
			return form(cloudfactroyOrder, model);
		}
		cloudfactroyOrderService.save(cloudfactroyOrder);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactroyOrder/?repage";
	}
	

	@RequestMapping(value = "audit")
	public String delete(String orderNo, RedirectAttributes redirectAttributes) {

		int i=cloudfactroyOrderService.update1(orderNo);
		if(i==1) {
		addMessage(redirectAttributes, "审核订单审核成功");
		} else {
			addMessage(redirectAttributes, "审核订单审核成功");
		}
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactroyOrder/?repage";
	}
}